# -*- coding: UTF-8 -*-
# 改变参数，观察结果
import random

from matplotlib import use
import match_SP
import OPT
import FCFS
import Dataset
from seller_SP import SellerAoISP
from buyer_SP import BuyerAoISP
from util import Util

# 参数
qseller = 15  # 卖家数量
qbuyer = 3  # 买家数量
budget = 80  # 初始预算
avground = 8  # 取平均次数
T = 50  # 时间段数量


# 工具类
util = Util()


def changeBudget():
    """
    改变预算值，绘制结果
    """
    x = []
    sprofit = []  # 数据源平均利益
    waoi_sp = []  # 论文算法aoi
    waoi_opt = []  # 最优算法aoi
    waoi_fcfs = []  # FCFS算法aoi
    for budget in range(40, 301, 20):
        print("budget:", budget)
        sp = 0
        ba_sp = 0
        ba_opt = 0
        ba_fcfs = 0
        for i in range(avground):
            print(i)
            s = []  # 卖家集合
            b = []  # 买家集合
            for ii in range(qseller):
                seller = SellerAoISP(ii, qbuyer, T)
                s.append(seller)
            for ii in range(qbuyer):
                buyer = BuyerAoISP(ii, T, budget)
                b.append(buyer)
            # 论文算法
            pdata, w_AoISP, buyeraoi_AoISP = match_SP.match_AoISP(T, s, b)
            # 最优算法
            w_OPT, buyeraoi_OPT = OPT.OPT(T, s, b)
            # FCFS算法
            w_FCFS, buyeraoi_FCFS = FCFS.FCFS(T, s, b)
            # 累加
            length = len(pdata['x'])
            sp += pdata['sellerProfit'][length-1]/avground
            ba_sp += sum(buyeraoi_AoISP)/avground
            ba_opt += sum(buyeraoi_OPT)/avground
            ba_fcfs += sum(buyeraoi_FCFS)/avground
        # 记录当前参数数据
        x.append(budget)
        sprofit.append(sp/qseller)
        waoi_sp.append(ba_sp)
        waoi_opt.append(ba_opt)
        waoi_fcfs.append(ba_fcfs)
        print([sp/qseller, ba_sp, ba_opt, ba_fcfs])
    util.plotThreeAoiData(x, waoi_sp, waoi_opt, waoi_fcfs,
                          'each buyer\'s budget', 'SP-AoI', 'OPT', 'FCFS', 20)
    util.plotDoubleData(x, sprofit, waoi_sp, 'each buyer\'s budget',
                        'average seller profit', 'AoI cost')


def changeSellerQ():
    """
    改变卖家数量，绘制结果
    """
    x = []
    sprofit = []  # 数据源平均利益
    waoi_sp = []  # 论文算法aoi
    waoi_opt = []  # 最优算法aoi
    waoi_fcfs = []  # FCFS算法aoi
    for qseller in range(8, 16, 2):
        print("qseller:", qseller)
        sp = 0
        ba_sp = 0
        ba_opt = 0
        ba_fcfs = 0
        for i in range(avground):
            print(i)
            s = []  # 卖家集合
            b = []  # 买家集合
            for ii in range(qseller):
                seller = SellerAoISP(ii, qbuyer, T)
                s.append(seller)
            for ii in range(qbuyer):
                buyer = BuyerAoISP(ii, T, budget)
                b.append(buyer)
            # 论文算法
            pdata, w_AoISP, buyeraoi_AoISP = match_SP.match_AoISP(T, s, b)
            # 最优算法
            w_OPT, buyeraoi_OPT = OPT.OPT(T, s, b)
            # FCFS算法
            w_FCFS, buyeraoi_FCFS = FCFS.FCFS(T, s, b)
            # 累加
            length = len(pdata['x'])
            sp += pdata['sellerProfit'][length-1]/avground
            ba_sp += sum(buyeraoi_AoISP)/avground
            ba_opt += sum(buyeraoi_OPT)/avground
            ba_fcfs += sum(buyeraoi_FCFS)/avground
        # 记录当前参数数据
        x.append(qseller)
        sprofit.append(sp/qseller)
        waoi_sp.append(ba_sp)
        waoi_opt.append(ba_opt)
        waoi_fcfs.append(ba_fcfs)
        print([sp/qseller, ba_sp, ba_opt, ba_fcfs])
    util.plotThreeAoiData(x, waoi_sp, waoi_opt, waoi_fcfs,
                          'number of sources', 'SP-AoI', 'OPT', 'FCFS')


def changeBudget_DS():
    """
    基于cab数据集，改变预算值，绘制结果
    """
    T = 1200
    qbuyer = 3
    qseller = 15
    x_budget = range(40, 251, 10)
    sprofit = []  # 数据源平均利益
    waoi_sp = []  # 论文算法aoi
    waoi_opt = []  # 最优算法aoi
    waoi_fcfs = []  # FCFS算法aoi
    for budget in x_budget:
        print("budget:", budget)
        sp = 0
        aoi_sp = 0
        aoi_opt = 0
        aoi_fcfs = 0
        for i in range(avground):
            print(i)
            # 论文算法
            s, b = Dataset.getDatasetSB(T, qseller, qbuyer, budget)
            pdata, w_AoISP, buyeraoi_AoISP = match_SP.match_AoISP(T, s, b)
            # 最优算法
            s, b = Dataset.getDatasetSB(T, qseller, qbuyer, budget)
            w_OPT, buyeraoi_OPT = OPT.OPT(T, s, b)
            # FCFS算法
            s, b = Dataset.getDatasetSB(T, qseller, qbuyer, budget)
            w_FCFS, buyeraoi_FCFS = FCFS.FCFS(T, s, b)
            # 累加
            length = len(pdata['x'])
            sp += pdata['sellerProfit'][length-1]/avground
            aoi_sp += sum(buyeraoi_AoISP)/avground
            aoi_opt += sum(buyeraoi_OPT)/avground
            aoi_fcfs += sum(buyeraoi_FCFS)/avground
            print(w_AoISP, w_OPT, w_FCFS)
        # 记录当前参数数据
        sprofit.append(sp/qseller)
        waoi_sp.append(aoi_sp)
        waoi_opt.append(aoi_opt)
        waoi_fcfs.append(aoi_fcfs)
        print(["result: ", sp/qseller, aoi_sp, aoi_opt, aoi_fcfs])
    # 绘图
    util.plotThreeAoiData_DS(x_budget, waoi_sp, waoi_opt, waoi_fcfs,
                             'each buyer\'s budget', 'SP-AoI', 'OPT', 'FCFS', 20)
    util.plotDoubleData_cab(x_budget, sprofit, waoi_sp, 'each buyer\'s budget',
                            'average sources profit', 'AoI cost')


if(__name__ == "__main__"):
    # changeBudget()
    changeBudget_DS()
    # changeSellerQ()
